package com.spsoft.workflow.controller;


import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.model.Result;
import com.spsoft.workflow.model.dto.bus.BusinessProcessDto;
import com.spsoft.workflow.model.dto.bus.FlowDataDto;
import com.spsoft.workflow.model.dto.bus.SaveAndSubmitDto;
import com.spsoft.workflow.model.dto.bus.handlingProcessDto;
import com.spsoft.workflow.model.vo.biz.ApplyFlowVo;
import com.spsoft.workflow.model.vo.biz.ProcessLogVo;
import com.spsoft.workflow.service.BusinessFlowService;
import com.spsoft.workflow.service.ProcessBusinessService;
import com.spsoft.workflow.service.WfProcessLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 业务流程（发起、办理、终止等
 * </p>
 *
 * @author dlchen
 * @since 2023-11-13
 */
@Api(tags = {"业务流程（发起、办理、终止等）"})
@RestController
@RequestMapping("/businessFlow")
@RequiredArgsConstructor
@Validated
@Slf4j
public class BusinessFlowController {

    private final BusinessFlowService  businessFlowService;
    private final WfProcessLogService wfProcessLogService;
    private final ProcessBusinessService processBusinessService;
    @ApiOperation("获取申请数据")
    @AnonymousAccess(value = "/getFlowData", method = RequestMethod.POST, permissionCode = "VMS_BUSINESS_FLOW_GET_FLOW_DATA")
    @Log(value = "获取申请数据", type = LogActionType.SELECT, menuName = LogMenuEnum.WORKFLOW)
    public Result<ApplyFlowVo> getFlowData(@Valid @RequestBody FlowDataDto q) {
        return businessFlowService.getFlowData(q);
    }

    @ApiOperation("保存流程&提交流程")
    @AnonymousAccess(value = "/saveOrSubmit", method = RequestMethod.POST, permissionCode = "VMS_BUSINESS_FLOW_SAVE_OR_SUBMIT")
    @Log(value = "保存流程&提交流程", type = LogActionType.UPDATE, menuName = LogMenuEnum.WORKFLOW)
    public Result saveOrSubmit(@Valid @RequestBody SaveAndSubmitDto s) {
        return businessFlowService.saveOrSubmit(s);
    }


    @ApiOperation("办理流程")
    @AnonymousAccess(value = "/handlingProcess", method = RequestMethod.POST, permissionCode = "VMS_BUSINESS_FLOW_HANDLING_PROCESS")
    @Log(value = "办理流程", type = LogActionType.UPDATE, menuName = LogMenuEnum.WORKFLOW)
    public Result handlingProcess(@Valid @RequestBody handlingProcessDto h) {
        return businessFlowService.handlingProcess(h);
    }

    @ApiOperation("终止流程")
    @AnonymousAccess(value = "/stopProcess", method = RequestMethod.POST, permissionCode = "VMS_BUSINESS_FLOW_STOP_PROCESS")
    @Log(value = "终止流程", type = LogActionType.CLOSE, menuName = LogMenuEnum.WORKFLOW)
    public Result stopProcess(@Valid @RequestBody BusinessProcessDto bus) {
        return businessFlowService.stopProcess(bus.getBusId());

    }

    @ApiOperation("获取下一个节点信息")
    @AnonymousAccess(value = "/getNextHandleInfo", method = RequestMethod.POST, permissionCode = "VMS_BUSINESS_FLOW_GET_NEXT_HANDLE_INFO")
    @Log(value = "获取下一个节点信息", type = LogActionType.SELECT, menuName = LogMenuEnum.WORKFLOW)
    public Result getNextHandleInfo(@Valid @RequestBody BusinessProcessDto bus) {
        return businessFlowService.getNextHandleInfo(bus.getBusId());
    }

    @ApiOperation("流程记录")
    @AnonymousAccess(value = "/processLog", method = RequestMethod.POST, permissionCode = "VMS_BUSINESS_FLOW_PROCESS_LOG")
    @Log(value = "流程记录", type = LogActionType.SELECT, menuName = LogMenuEnum.WORKFLOW)
    public Result<List<ProcessLogVo>> processLog(@RequestBody @Valid BusinessProcessDto bus) {
        return wfProcessLogService.queryProcessLog(bus);
    }
}

